This module provides types and useful methods for working with IPv4 and IPv6 network addresses, commonly called IP prefixes. The new IpNet
, Ipv4Net
, and Ipv6Net
types build on the existing IpAddr
, Ipv4Addr
, and Ipv6Addr
types already provided in Rust's standard library and align to their design to stay consistent.
The module also provides the IpSubnets
, Ipv4Subnets
, and Ipv6Subnets
types for iterating over the subnets contained in an IP address range. The IpAddrRange
, Ipv4AddrRange
, and Ipv6AddrRange
types for iterating over IP addresses in a range. And traits that extend Ipv4Addr
and Ipv6Addr
with methods for addition, subtraction, bitwise-and, and bitwise-or operations that are missing in Rust's standard library.
The module only uses stable features so it is guaranteed to compile using the stable toolchain. Tests aim for thorough coverage and can be found in both the test modules and doctests. Please file an issue on GitHub if you have any problems, requests, or suggested improvements.
Read the documentation for the full details. And find it on Crates.io.
Release 2.0 requirements
Release 2.0 requires Rust 1.26 or later. Release 1.0 used a custom emulated 128-bit integer type (Emu128
) to fully support IPv6 addresses. This has been replaced with Rust's built-in 128-bit integer, which is now stable as of Rust 1.26. There are reports of issues using Rust's 128-bit integers on some targets (e.g. Emscripten). If you have issues on your chosen target, please continue to use the 1.0 release until that has been resolved.
Examples
Create a network address and print the hostmask and netmask
extern crate ipnet;
use ;
use FromStr;
use ;
Subdivide an existing IP network into smaller subnets
extern crate ipnet;
use Ipv4Net;
Iterate over the valid subnets between two IPv4 addresses
extern crate ipnet;
use Ipv4Addr;
use Ipv4Subnets;
Aggregate a list of IP prefixes
extern crate ipnet;
use IpNet;
Future
- Implementing
std::ops::{Add, Sub, BitAnd, BitOr}
forIpv4Addr
andIpv6Addr
would be useful as these are common operations on IP addresses. If done, the extension traits provided in this module would be removed and the major version incremented. Implementing these requires a change to the standard library. I've started a thread on this topic on the Rust Internals discussion board. - The results of
hosts()
and potentiallysubnets()
should be represented as aRange
rather than the customIpAddrRange
andIpSubnets
types provided in this module. This requires the target types to haveAdd
andStep
implemented for them. ImplementingAdd
forIpAddr
,Ipv4Addr
, andIpv6Addr
requires a change to the standard library (see above). AndStep
is still unstable so exploring this will also wait until it has stablized.
License
Copyright (c) 2017, Juniper Networks, Inc. All rights reserved.
This code is licensed to you under either the MIT License or Apache License, Version 2.0 at your choice (the "License"). You may not use this code except in compliance with the License. This code is not an official Juniper product. You can obtain a copy of the License at: https://opensource.org/licenses/MIT or http://www.apache.org/licenses/LICENSE-2.0